package com.lag.transfer.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

/**
 * 事务管理器类，负责手动事务的开始，提交，回滚
 *
 * @author June
 * @since 2020年12月
 */
//@Component("transactionManager")
public class TransactionManager {

    //@Autowired
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    // 开启手动事务控制
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrThreadConnection().setAutoCommit(false);
    }


    // 提交事务
    public void commit() throws SQLException {
        connectionUtils.getCurrThreadConnection().commit();
    }


    // 回滚事务
    public void rollback() throws SQLException {
        connectionUtils.getCurrThreadConnection().rollback();
    }
}
